(function ($) {
  var defaults = {
    messageStart : "Please wait... ",
    messageEnd : "loading.",
    successMessage : "The operation was successful.",
    successTitle : "Success!",
    errorMessage : "An error has occurred.",
    errorTitle : "Error!",
    warningMessage : "Warning",
    warningTitle : "Warning!",
    confirmMessage : "Are you sure you want to proceed",
    confirmTitle : "Confirmation!"
  };

  var defaultPluginSettings = {
    css :{ },
    cssDefaultOverride : { //customization needed here to make plugin style configurable solely based on css
      width:		"25%",
      border:		"none",
		  backgroundColor: "none",
      cursor : "auto"
    },
    cssSideOverride : {
      fadeIn: 700,
      fadeOut: 700,
      showOverlay: false,
      centerY: false,
      css : {
        width: '250px',
        top: '100px',
        left: '',
        right: '10px'
      }
    },
    overlayCSS : {
      cursor : "auto"
    },
    button : {
      okLabel : "Ok",
      cancelLabel : "Cancel"
    },
    confirmButtons : {
      ok :{label : "Ok", onclick : function(){$.TWS_Overlay("hide");}},
      cancel:{label : "Cancel", onclick : function(){$.TWS_Overlay("hide");}  }
    }
  };
  
  //TODO - This is not clean here
  $(".TWS_OverlayClose").live("click.tws", function(){
     $.unblockUI();
     return false;
  });

  $.TWS_Overlay = function (inputOptions) {
    // Code to detrimine if a method or constructor call.
    var arg = arguments[0];
    if (typeof arg === "string") {
      var func = methods["twsOverlay_" + arguments[0]];
      if (typeof func === "function") {
        return func.apply(this, Array.prototype.slice.call(arguments,1));
      } else {
        $.TWS_Console("warn", "TWS_Overlay: \"%s\" is not supported.", arg);
        return false;
      }
    } else {
      $.TWS_Console("warn", "TWS_Overlay: must specify method.", arg);
    }
  }
  //options
  // var options = $.extend(defaults, inputOptions);
  var methods = {
    twsOverlay_show: function (input) {
      //TODO Check for modal parameter
      var displayMessage = defaults.messageStart + defaults.messageEnd;
      var inputOptions = typeof input === "object" ? input : {};
      if(input){
        if (typeof input === "string"){
          displayMessage = defaults.messageStart + input;
        } else if(typeof input ==="object" && input.message){
          displayMessage = input.message;          
        }
      }
      $.blockUI(
        $.extend(true, defaultPluginSettings, inputOptions, {message: "<div class='TWS_Overlay'><div class='TWS_OverlayContent'><p>" + displayMessage + "</p></div></div>"})
      );
    },
    twsOverlay_hide: function()  {
      $.unblockUI();
    },

    twsOverlay_success: function(input) {
      var displayMessage = typeof input === "string" ? input : input && input.message ? input.message : defaults.successMessage;
      var inputOptions = typeof input === "object" ? input : {};
      var $blockUI = $.blockUI(
        $.extend(true, defaultPluginSettings, inputOptions, {message: "<div class='TWS_Overlay'><div class='TWS_OverlaySuccessTitle'><span class='TWS_OverlayTitleText'>"+ (input && input.title ? input.title : defaults.successTitle )+"</span><span class='TWS_OverlayClose' title='Close'></span></div><div class='TWS_SuccessText'><div class='TWS_OverlayText'>" + displayMessage + "</div></div><div><input type='button' class='TWS_OverlayButton'/></div></div>"})
      );
      setButtonOptions($blockUI, inputOptions);
    },

    twsOverlay_error: function(input) {
      var displayMessage = typeof input === "string" ? input : input && input.message ? input.message : defaults.errorMessage;
      var inputOptions = typeof input === "object" ? input : {};
      var $blockUI = $.blockUI(
        $.extend(true, defaultPluginSettings, inputOptions, {message: "<div class='TWS_Overlay'><div class='TWS_OverlayErrorTitle'><span class='TWS_OverlayTitleText'>"+ (input && input.title ? input.title : defaults.errorTitle )+"</span><span class='TWS_OverlayClose' title='Close'></span></div><div class='TWS_ErrorText'><div class='TWS_OverlayText'>" + displayMessage + "</div></div><div><input type='button' class='TWS_OverlayButton'/></div></div>"})
      );
        setButtonOptions($blockUI, inputOptions);
    },

    twsOverlay_warning: function(input) {
      if(input){
        var displayMessage = typeof input === "string" ? input : input && input.message ? input.message : defaults.warningMessage;
        var inputOptions = typeof input === "object" ? input : {};
        var $blockUI = $.blockUI(
          $.extend(true, defaultPluginSettings, inputOptions, {message: "<div class='TWS_Overlay'><div class='TWS_OverlayWarningTitle'><span class='TWS_OverlayTitleText'>"+ (input && input.title ? input.title : defaults.warningTitle )+"</span><span class='TWS_OverlayClose' title='Close'></span></div><div class='TWS_WarningText'><div class='TWS_OverlayText'>" + displayMessage + "</div></div><div><input type='button' class='TWS_OverlayButton'/></div></div>"})
        );
        setButtonOptions($blockUI, inputOptions);
      }
    },
    twsOverlay_confirm: function(input) {
      if(input){
        var displayMessage = typeof input === "string" ? input : input && input.message ? input.message : defaults.confirmMessage;
        var inputOptions = typeof input === "object" ? input : {};
        var $blockUI =$.blockUI(
          $.extend(true, defaultPluginSettings, inputOptions, {message: "<div class='TWS_Overlay'><div class='TWS_OverlayConfirmTitle'><span class='TWS_OverlayTitleText'>"+ (input && input.title ? input.title : defaults.confirmTitle )+"</span><span class='TWS_OverlayClose' title='Close'></span></div><div class='TWS_ConfirmText'><div class='TWS_OverlayText'>" + displayMessage + "</div></div><div><input type='button' class='TWS_OverlayButton'/></div><div><input type='button' class='TWS_OverlayButton'/></div></div>"})
        );
        setButtonOptions($blockUI, inputOptions, true);
      }
    },
    twsOverlay_side: function(input) {
        var displayMessage = typeof input === "string" ? input : input && input.message ? input.message : defaults.messageStart;
        var inputOptions = typeof input === "object" ? input : {};
        $.blockUI(
          $.extend(true, {}, defaultPluginSettings,defaultPluginSettings.cssSideOverride, inputOptions ,  {message: "<div class='TWS_OverlaySide'>" + displayMessage + "</div>"})
        );
    }
  }

  function setButtonOptions($overlay, inputOptions, multipleButtons) {
    if (typeof $overlay !=="jQuery" && inputOptions) {
      if (multipleButtons) {
        var buttonOptions = $.extend(true, {}, defaultPluginSettings.confirmButtons, inputOptions.buttons);
        $(".TWS_OverlayButton:eq(0)", $overlay).button({label : buttonOptions.ok.label || defaultPluginSettings.button.okLabel}).click(typeof buttonOptions.ok.onclick==="function" ? buttonOptions.ok.onclick : function() {$.TWS_Overlay("hide")});
        $(".TWS_OverlayButton:eq(1)", $overlay).button({label : buttonOptions.cancel.label || defaultPluginSettings.button.cancelLabel}).click(typeof buttonOptions.cancel.onclick==="function" ? buttonOptions.cancel.onclick : function() {$.TWS_Overlay("hide")});
      } else {
        $(".TWS_OverlayButton", $overlay).button({label : inputOptions.label || defaultPluginSettings.button.okLabel}).click(inputOptions.onclick && typeof inputOptions.onclick==="function" ? inputOptions.onclick : function() {$.TWS_Overlay("hide")});
      }
    }
  }

})(jQuery);
